package com.scvtc.service.teacher.impl;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scvtc.domain.ResResult;
import com.scvtc.domain.dto.teacher.base.TeacherBaseDto;
import com.scvtc.domain.entity.teacher.TeacherBase;
import com.scvtc.domain.enums.OpType;
import com.scvtc.domain.vo.PageVo;
import com.scvtc.mapper.teacher.TeacherBaseMapper;
import com.scvtc.service.teacher.TeacherBaseService;
import com.scvtc.utils.BeanCopyUtils;
import com.scvtc.utils.DictionaryUtil;
import com.scvtc.utils.ExportExcelUtil;
import com.scvtc.utils.LikeQueryUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.scvtc.constants.ExportExcelConstants.TEACHER_BASE_FILENAME;
import static com.scvtc.constants.RedisConstant.TEACHER_BASE;


/**
 * 教师基本信息(TeacherBase)表服务实现类
 *
 * @author thy
 * @since 2022-04-13 17:38:30
 */
@Service("teacherBaseService")
public class TeacherBaseServiceImpl extends ServiceImpl<TeacherBaseMapper, TeacherBase> implements TeacherBaseService {

    @Autowired
    private DictionaryUtil dictionaryUtil;

    /**
     * 分页查询
     *
     * @param teacherBaseDto
     * @param current
     * @param size
     * @return
     */
    public ResResult<PageVo<List<TeacherBaseDto>>> getAllTeacher(Long current, Long size, TeacherBaseDto teacherBaseDto) {
        // 查询
        Page<TeacherBase> basePage = page(new Page<>(current, size),
                LikeQueryUtil.likeQuery(teacherBaseDto, TeacherBase.class)
                        .lambda()
                        .orderByDesc(TeacherBase::getCreateTime));

        List<TeacherBaseDto> studentBaseVos = BeanCopyUtils.copyBean(basePage.getRecords()
                , TeacherBaseDto.class);

        PageVo<List<TeacherBaseDto>> pageVo = new PageVo<>();
        pageVo.setRow(studentBaseVos);
        pageVo.setTotal(basePage.getTotal());

        return ResResult.noMsgSuccess(pageVo);
    }

    /**
     * 删除教师
     *
     * @param id
     * @return
     */
    @Override
    public ResResult<?> deleteTeacherBase(Long id) {
        // 删除教师基本信息
        if (removeById(id)) {
            dictionaryUtil.refreshData(new TeacherBase().setId(id), TEACHER_BASE, OpType.DELETE);
            return ResResult.success();
        }
        return ResResult.fail();
    }

    /**
     * 删除多个教师
     *
     * @param ids
     * @return
     */
    @Override
    public ResResult<?> deleteMultipleTeacherBase(Long[] ids) {
        if (removeByIds(Arrays.asList(ids))) {
            dictionaryUtil.deleteMultiple(ids, TEACHER_BASE, OpType.DELETE_MULTIPLE);
            return ResResult.success();
        }
        return ResResult.fail();
    }

    /**
     * 添加一个教师
     *
     * @param teacherBaseDto
     * @return
     */
    @Override
    public ResResult<?> addTeacherBase(TeacherBaseDto teacherBaseDto) {
        // 不需要传入 id
        TeacherBase teacherBase = BeanCopyUtils.copyBean(teacherBaseDto, TeacherBase.class).setId(null);
        if (save(teacherBase)) {
            dictionaryUtil.refreshData(teacherBase, TEACHER_BASE, OpType.SAVE);
            return ResResult.success(teacherBase.getId());
        }
        return ResResult.fail();
    }

    /**
     * 修改教师信息
     *
     * @param teacherBaseDto
     * @return
     */
    @SneakyThrows
    @Override
    public ResResult<?> updateTeacherBase(TeacherBaseDto teacherBaseDto) {
        TeacherBase teacherBase = BeanCopyUtils.copyBean(teacherBaseDto, TeacherBase.class);
        // 修改失败
        if (updateById(teacherBase)) {
            dictionaryUtil.refreshData(teacherBase, TEACHER_BASE, OpType.UPDATE);
            return ResResult.success();
        }
        // 返回失败
        return ResResult.fail();
    }

    /**
     * 教师表导出excel
     *
     * @param response
     */
    @Override
    public void exportExcel(HttpServletResponse response,Long[] ids) {
        // 先查询
        List<TeacherBase> list=list(
                new LambdaQueryWrapper<TeacherBase>()
                        .in(TeacherBase::getId, Arrays.asList(ids))
        );
        // BeanCopy 拷贝
        List<TeacherBaseDto> teacherBaseList = BeanCopyUtils.copyBean(list, TeacherBaseDto.class);
        // 使用 ExportExcelUtil 导出 Excel
        ExportExcelUtil.exportExcel(
                new ExportParams("教师基本信息", "基本信息"),
                TeacherBaseDto.class,
                teacherBaseList,
                response,
                TEACHER_BASE_FILENAME
        );
    }

    /**
     * 教师等级（charts 图）
     *
     * @return
     */
    @Override
    public ResResult<?> chartDegree() {
        List<Map<String, Object>> chartDegree = getBaseMapper().chartDegree();
        return ResResult.noMsgSuccess(chartDegree);
    }
}

